package com.keydom.ropeskipping.utils;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogLocalUtils {

    private static final String TAG = "LogLocalUtils";

    private static String basePath = Environment.getExternalStorageDirectory().getPath()
            + "/Android/rope/log/";

    /**
     * 开启全局错误日志捕获
     */
    public static void openErrorCatch() {
        CrashHandler.getInstance().init();
    }

    /**
     * 记录用户在什么时间启动的App
     * 登录成功进入菜单界面记录一次
     */
    public static void startAppRecord() {
        writeLogToFile("----------> 登录成功，进入到菜单界面了... <----------");
    }

    /**
     * 保存日志信息到log.txt文件中
     * 每次调用往log.txt文件中添加信息
     *
     * @param logMessage 日志内容
     */
    public static void writeLogToFile(String logMessage) {

        try {
            //新建多级目录
            File sdCardDir = new File(basePath);
            if (!sdCardDir.exists() && !sdCardDir.mkdirs()) {
                try {
                    sdCardDir.createNewFile();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d(TAG, "writeLogToFile-sdCardDir.createNewFile():" + e.getMessage());
                }
            }
            //一个月创建一次日志文件
            String yearMonth = new SimpleDateFormat("yyyyMM").format(new Date());
            String fileName = "log-" + yearMonth + ".txt";
            String logFilePath = basePath + fileName;
            FileOutputStream outputStream = new FileOutputStream(new File(logFilePath), true);
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date());
            String message =
                    "<---------------------------start---------------------------->"
                            + "\n"
                            + "\n"
                            + time + "\n"
                            + "日志信息：" + logMessage + "\n"
                            + "\n"
                            + "<---------------------------end---------------------------->"
                            + "\n"
                            + "\n"
                            + "\n"
                            + "\n";
            outputStream.write(message.getBytes());
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "writeLogToFile-error: " + e.getMessage());
        }

    }


    /**
     * 保存异常信息文件到sdcard中
     * 每次调用创建新文件
     *
     * @param errorLog 日志内容
     */
    public static void writeLogToSdCard(String errorLog) {

        String sdPath = basePath + "/error_log";
        //新建多级目录
        File sdCardDir = new File(sdPath);

        if (!sdCardDir.exists() && !sdCardDir.mkdirs()) {

            try {
                sdCardDir.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
                Log.d(TAG, "sdCardDir.createNewFile()-error:" + e.getMessage());
            }

        }

        try {
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
                    .format(new Date());
            String fileName = time + ".txt";
            //新建文件
            File saveFile = new File(sdCardDir, fileName);

            if (!saveFile.exists()) {
                saveFile.createNewFile();
            }

            final FileOutputStream outStream = new FileOutputStream(saveFile);

            if (!TextUtils.isEmpty(errorLog)) {
                String message =
                        "<---------------------------start---------------------------->"
                                + "\n"
                                + "\n"
                                + time + "\n"
                                + "日志信息：" + errorLog + "\n"
                                + "\n"
                                + "<---------------------------end---------------------------->"
                                + "\n";
                outStream.write(message.getBytes());
                outStream.close();
            }
            Log.d(TAG, "日志输出成功! ");
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "流写入错误:" + e.toString());
        }
    }
}
